// pages/logs/logs.js
const app = getApp();
const utils = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ismodify:false,
    userInfo:{},
    timeShow:false,
    showYear:true,
    initDate: '1995-01-01',
    
    birthdayDefault: '',
    birthdayTime: '',
    birthdayLunarStr: '',
    showSex:false,
    realName:'',
  },
  modify(){
    this.setData({
      ismodify:true
    })
  },
  //头像
  changeAvatar: function () {
    var that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        const filepath = res.tempFilePaths[0]
        const src = filepath
        console.log("原地址", src)
        wx.navigateTo({
          url: `../upload/upload?src=${src}`
        })
      }
    })
  },
  //姓名
  realNameBindtap: function (e) {
    if (e.detail.value.trim().length == 0) {
      this.setData({
        realName: ''
      })
    } else {
      this.setData({
        realName: e.detail.value.replace(/^\s+|\s+$/g,'')
      })
    }
    this.toggle("userInfo.realName", this.data.realName);
  },
  timeShow(){
    this.setData({
      timeShow:true,
    })
  },
  submit: function (e) {
    console.log(e);
    console.log('日期类型：' + e.detail.dateType)
    console.log('是否显示年份：' + e.detail.showYear)
    console.log('日期字符串：' + e.detail.dateStr)
    if (e.detail.dateType == '公历') {
      this.setData({
        birthdayTime: e.detail.dateStr + ' ' + '00:00:00',
        birthdayDefault: 0,
        showYear: e.detail.showYear
      })
      console.log('公历判断', this.data.birthdayTime, this.data.initDate)
    } else {
      console.log('农历', 11111)
      this.setData({
        birthdayLunarStr: e.detail.dateStr,
        birthdayDefault: 1
      })
    }
    if (this.data.birthdayDefault == 0) {
      if(this.data.showYear){
        this.setData({
          'userInfo.birthday': utils.formatTime4(this.data.birthdayTime).substring(0, 10)
        })
      }else{
        this.setData({
          'userInfo.birthday': utils.formatTime4('1995-'+this.data.birthdayTime).substring(0, 10),
          birthdayTime: '1995-' + this.data.birthdayTime
        })
      }
      console.log(this.data.userInfo.birthday)
    } else {
      this.setData({
        'userInfo.birthday': this.data.birthdayLunarStr.substring(9, 13)
      })
      console.log(this.data.userInfo.birthday)
    }

  },
  showsex(){
    this.setData({
      showSex:true
    })
  },
  onClose(){
    this.setData({
      showSex: false
    })
  },
  selectToItemSex: function (event) {
    var that = this;
    var data = event.currentTarget.dataset;
    that.toggle("userInfo.sex", data.id);
    // that.saveSex(data.id);
    that.setData({
      sex: data.id,
      showSex:false
    })
  },
  cancel(){
    this.setData({
      ismodify: false
    })
    this.onShow()
  },
  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },
  sure(){
    var that = this
    if (that.data.userInfo.realName.trim()==''){
      wx.showToast({
        title: '昵称不能为空',
        icon:"none"
      })
      return
    }else{
    app.req({
      url:'blessmini/user/updateUserInformation',
      data:{
        realName:that.data.userInfo.realName,
        sex:that.data.userInfo.sex,
        avatarUrl:that.data.userInfo.avatarUrl,
        birthday: that.data.birthdayTime,
        birthdayDefault:0,
      },
      method:'post',
      success:function(res){
        if(res.code==200){
          wx.showToast({
            title: '修改成功',
          })
          that.setData({
            ismodify:false,
          })
        }
      }
    })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    app.getUserInfo(function (userInfo) {
      that.toggle("userInfo", userInfo);
    });
    if(that.data.userInfo.birthday){
      that.setData({
        'userInfo.birthday': utils.formatTime4(that.data.userInfo.birthday).substring(0, 10)
      })
    }
    console.log('个人信息', that.data.userInfo)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})